import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import pearsonr
from stock.utils.get_stock_price import get_stock_price
from stock.utils.price_utils import merge_same_day
from sklearn.preprocessing import MinMaxScaler


# 设置中文显示
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False




def visualize_results(list1, list2):
    scaler = MinMaxScaler()
    list1_2d = np.array(list1).reshape(-1, 1)  # 转换为 2D 数组 (n_samples, n_features)
    scaler = MinMaxScaler()
    list1 = scaler.fit_transform(list1_2d).flatten()  # 还原为 1D

    list2_2d = np.array(list2).reshape(-1, 1)  # 转换为 2D 数组 (n_samples, n_features)
    list2 = scaler.fit_transform(list2_2d).flatten()  # 还原为 1D




    plt.figure(figsize=(10, 6))  # 设置图表大小

    # 绘制两条折线
    plt.plot(list1, label='List 1', marker='o', color='blue')   # 蓝色实线，带圆圈标记
    plt.plot(list2, label='List 2', marker='s', color='red', linestyle='--')  # 红色虚线，带方形标记

    # 添加标题和标签
    plt.title('Comparison of List1 and List2', fontsize=14)
    plt.xlabel('Index', fontsize=12)
    plt.ylabel('Value', fontsize=12)

    # 添加图例和网格
    plt.legend(fontsize=10)
    plt.grid(True, linestyle='--', alpha=0.6)

    # 显示图表
    plt.show()

if __name__ == "__main__":

    code = '101.HG00Y'
    klt = '101'
    count = '50'
    end = '20501010'
    list1 = get_stock_price(code, klt, count, end)

#江西铜业
    code = '1.600362'
#云南铜业
    # code = '0.000878'
    #
    # code = '1.600738'
    list2 = get_stock_price(code, klt, count, end)



    list1, list2 = merge_same_day(list1, list2)

    # 计算皮尔逊相关系数
    corr, p_value = pearsonr(list1, list2)
    print("Pearson 相关系数:", corr)  # 输出: 1.0
    print("P 值:", p_value)  # 输出: 0.0（显著相关） '显著性': '显著' if p_value < 0.05 else '不显著'
    visualize_results(list1, list2)